<template>
  <div class="dialog">
    <el-dialog width="45%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" :disabled="disabled" label-width="110px">
        <el-form-item label="角色名称：" prop="roleName">
          <el-col :span="12">
            <el-input v-model="form.roleName" placeholder="请输入" maxlength="20" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="角色描述：" prop="roleDesc">
          <el-col :span="12">
            <el-input v-model="form.roleDesc" placeholder="请输入" maxlength="40" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="角色权限：" prop="menuIds">
          <el-row>
            <el-tree
              ref="treeNode"
              :data="menuList"
              node-key="id"
              :props="defaultProps"
              :default-checked-keys="defaultKeys"
              show-checkbox
              clearable
              style="height:300px;overflow-y:auto;"
              @check-change="handleCheckChange"
            />
          </el-row>
        </el-form-item>
      </el-form>
      <div v-if="type !== 'detail'" slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMenuList, addRole, editRole, getRoleDetail } from '@/api/system'
export default {
  name: 'AddRole',
  props: {
    type: {
      type: String,
      default: 'add' // add:新增  detail:详情  edit:编辑
    },
    id: {
      type: [String, Number],
      default: ''
    }
  },

  data() {
    return {
      defaultKeys: [], // 默认选中权限
      menuList: [],
      defaultProps: {
        children: 'childMenuEntities',
        label: 'name',
        disabled: this.isDisabledFn
      },
      loading: false,
      form: {
        roleName: '',
        roleDesc: '',
        menuIds: []
      },
      rules: {
        roleName: [{ required: true, message: '请输入角色名称', trigger: ['change', 'blur'] }],
        menuIds: [{ required: true, message: '请选择关联权限', trigger: ['change'] }]
      }
    }
  },

  computed: {
    title() {
      if (this.type === 'detail') {
        return '角色详情'
      } else if (this.type === 'edit') {
        return '编辑角色'
      } else {
        return '新增角色'
      }
    },
    disabled() {
      return this.type === 'detail'
    }
  },

  created() {
    if (this.id) {
      this.getDetail()
    }
    this.getTreeList()
  },

  methods: {

    // 判断tree是否禁用
    isDisabledFn() {
      return this.disabled
    },

    // 查询菜单列表
    getTreeList() {
      getMenuList().then(res => {
        this.menuList = res || []
      }).catch(() => {})
    },

    // 获取详情
    getDetail() {
      const data = {
        id: this.id
      }
      getRoleDetail(data).then(res => {
        const { roleName, roleDesc, menuResponses = [] } = res
        const menuIds = []
        menuResponses.map(item => {
          menuIds.push(item.id)
        })
        this.form = {
          roleName,
          roleDesc,
          menuIds
        }
        this.defaultKeys = menuIds
      }).catch(() => {
      })
    },

    handleCheckChange() {
      this.form.menuIds = this.$refs.treeNode.getCheckedKeys()
    },

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      if (this.type === 'add') {
        this.addHandle()
      } else {
        this.editHandle()
      }
    },

    // 新增
    addHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form
          }
          addRole(data).then(() => {
            this.$message.success('新增成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    // 编辑
    editHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form,
            id: this.id
          }
          editRole(data).then(() => {
            this.$message.success('编辑成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped></style>
